package me.ahoukuse.clipboard;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.concurrent.ExecutorService;



class MangerServers implements View.OnClickListener {

    private MainActivity activity;

    MangerServers(MainActivity act){
        activity = act;
    }

    @Override
    public void onClick(View view) {
        Button bt = (Button) view;
        EditText editText = activity.findViewById(R.id.IPAddr);
        EditText portText = activity.findViewById(R.id.portText);
        String IPv4 = editText.getText().toString();
        String port_S = portText.getText().toString();
        if (IPv4.equals("") || port_S.equals("")){
            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            builder.setMessage(R.string.diglog_title_none_error)
                    .setTitle(R.string.diglog_title_hi)
                    .setPositiveButton(R.string.dialog_confirm,null);
            AlertDialog dialog = builder.create();
            dialog.show();
            return;
        }
        if(!activity.buttonStat){
            activity.buttonStat = true;
            bt.setText("停止");

            String Sock_Addr = IPv4+":"+port_S;
            File file = new File(activity.getFilesDir(),activity.config);
            try{
                FileWriter writer = new FileWriter(file);
                writer.write(Sock_Addr);
                writer.close();
            } catch (IOException e){
                e.printStackTrace();
            }
            int port = Integer.parseInt(port_S);
            editText.setEnabled(false);
            portText.setEnabled(false);
            StartCoreService(IPv4,port);
        }
        else {
            activity.buttonStat = false;
            editText.setEnabled(true);
            portText.setEnabled(true);
            bt.setText("开始");
            StopCoreService();
        }
    }

    private void StopCoreService() {
        Intent intent = new Intent(activity,BackService.class);
        activity.stopService(intent);
    }

    private void StartCoreService(String IP,int Port) {
        Intent intent = new Intent(activity,BackService.class);
        intent.putExtra("IP",IP);
        intent.putExtra("Port",Port);
        activity.startService(intent);
    }
}


public class MainActivity extends AppCompatActivity {
    protected String TAG = "DEBUG:";
    protected Button button;
    protected EditText IP;
    public boolean buttonStat = false;
    public String config = "config.cig";
    private ExecutorService executorService = null;
    private float displayDPI;
    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = findViewById(R.id.confirm);
        button.setOnClickListener(new MangerServers(this));
        displayDPI =  this.getResources().getDisplayMetrics().densityDpi;
        File file = new File(this.getFilesDir(),config);
        boolean first = false;
        String configString = null;
        String[] SockAddr = null;
        try {
            if(file.exists()){
                FileReader fileReader = new FileReader(file);
                BufferedReader bufferedReader = new BufferedReader(fileReader);
                configString = bufferedReader.readLine();
                if(configString != null){
                    SockAddr = configString.split(":");
                }
            }else {
                file.createNewFile();
                first = true;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        IP = findViewById(R.id.IPAddr);
        EditText PORT = findViewById(R.id.portText);
        if(SockAddr != null){
            IP.setText(SockAddr[0]);
            PORT.setText(SockAddr[1]);
        }
        Log.d(TAG, "onCreate");
        if(first){
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage(R.string.dialog_context)
                    .setTitle(R.string.dialog_title)
                    .setPositiveButton(R.string.dialog_confirm,null);
            AlertDialog dialog = builder.create();
            dialog.show();
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.debug_info:
                Intent debug = new Intent(this,DebugInfoActivity.class);
                startActivity(debug);
                return true;
            case R.id.about:
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause");

    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop");

    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG, "onRestart");

    }

    @Override
    protected void onStart() {
        super.onStart();

    }
}
